package io.gitee.loulan_yxq.owner.poi.excel;

import io.gitee.loulan_yxq.owner.core.tool.ObjectTool;
import io.gitee.loulan_yxq.owner.poi.excel.entity.ExcelExportConfiguration;
import io.gitee.loulan_yxq.owner.poi.excel.enums.ExcelTypeEnum;
import io.gitee.loulan_yxq.owner.poi.excel.exception.ExcelException;
import io.gitee.loulan_yxq.owner.poi.excel.tool.ExcelHWriter;
import io.gitee.loulan_yxq.owner.poi.excel.tool.ExcelWriter;
import io.gitee.loulan_yxq.owner.poi.excel.tool.ExcelXWriter;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/*********************************************************
 ** 表格操作得工具类
 ** <br><br>
 ** Date: Created in 2022/2/7  19:49
 ** @author loulan
 ** @version 0.0.0
 *********************************************************/
public class ExcelTool {

    /**
     * 导出excel (表头为对象的属性名称)
     * 默认导出xls
     *
     * @param os        输出流
     * @param data      要写数据的list集合
     * @param sheetName 工作簿得名称
     * @author :loulan
     */
    public static void export(OutputStream os, String sheetName, List<?> data) {
        export(os, sheetName, data, ExcelTypeEnum.XLS);
    }

    /**
     * 导出excel (表头为对象的属性名称)
     *
     * @param os        输出流
     * @param data      要写数据的list集合
     * @param sheetName 工作簿得名称
     * @param excelType excel表的类型，xls或者xlsx
     * @author :loulan
     */
    public static void export(OutputStream os, String sheetName, List<?> data, ExcelTypeEnum excelType) {
        export(os, null, sheetName, data, excelType);
    }

    /**
     * 导出excel(可配置表头)
     * 默认导出xls
     *
     * @param os          输出流
     * @param data        要写数据的list集合
     * @param headerAlias 表头对应的别名，这个是对应list数据中对象的属性对应得名称，比如指定一个map数据为，key='uername',value='用户名'
     * @param sheetName   工作簿得名称
     * @author :loulan
     */
    public static void export(OutputStream os, Map<String, String> headerAlias, String sheetName, List<?> data) {
        export(os, headerAlias, sheetName, data, ExcelTypeEnum.XLS);
    }

    /**
     * 导出excel(可配置表头)
     *
     * @param os          输出流
     * @param data        要写数据的list集合
     * @param headerAlias 表头对应的别名，这个是对应list数据中对象的属性对应得名称，比如指定一个map数据为，key='uername',value='用户名'
     * @param sheetName   工作簿得名称
     * @param excelType   excel表的类型，xls或者xlsx
     * @author :loulan
     */
    public static void export(OutputStream os, Map<String, String> headerAlias, String sheetName, List<?> data, ExcelTypeEnum excelType) {
        if (ObjectTool.isNull(excelType)) {
            // 设置导出表的默认类型
            excelType = ExcelTypeEnum.XLS;
        }

        ExcelWriter writer = null;
        if (ExcelTypeEnum.XLS.getCode().equals(excelType.getCode())) {
            writer = ExcelHWriter.create();
        } else if (ExcelTypeEnum.XLSX.getCode().equals(excelType.getCode())) {
            writer = ExcelXWriter.create();
        } else {
            throw new ExcelException("the excelType is error!");
        }
        writer.write(os, headerAlias, sheetName, data);
        writer.close();
    }

    /**
     * 导出excel(可配置,默认导出xls)
     *
     * @param exportConfiguration excel导出配置
     * @author :loulan
     */
    public static void export(ExcelExportConfiguration exportConfiguration) {
        export(exportConfiguration, ExcelTypeEnum.XLS);
    }

    /**
     * 导出excel(可配置)
     *
     * @param exportConfiguration excel导出配置
     * @param excelType           excel表的类型，xls或者xlsx
     * @author :loulan
     */
    public static void export(ExcelExportConfiguration exportConfiguration, ExcelTypeEnum excelType) {
        if (ObjectTool.isNull(excelType)) {
            // 设置导出表的默认类型
            excelType = ExcelTypeEnum.XLS;
        }

        ExcelWriter writer = null;
        if (ExcelTypeEnum.XLS.getCode().equals(excelType.getCode())) {
            writer = ExcelHWriter.create();
        } else if (ExcelTypeEnum.XLSX.getCode().equals(excelType.getCode())) {
            writer = ExcelXWriter.create();
        } else {
            throw new ExcelException("the excelType is error!");
        }
        writer.write(exportConfiguration);
        writer.close();
    }
}
